package com.qiwenshare.file.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qiwenshare.common.anno.MyLog;
import com.qiwenshare.common.result.RestResult;
import com.qiwenshare.file.component.InitData;
import com.qiwenshare.file.domain.Department;
import com.qiwenshare.file.api.IDepartmentService;
import com.qiwenshare.file.domain.Notice;
import com.qiwenshare.file.domain.user.UserBean;
import com.qiwenshare.file.dto.department.AddDepartmentDTO;
import com.qiwenshare.file.dto.department.DeleteDepartmentDTO;
import com.qiwenshare.file.dto.department.ModifyDepartmentDTO;
import com.qiwenshare.file.dto.notice.NoticeListDTO;
import com.qiwenshare.file.dto.user.AddUserDTO;
import com.qiwenshare.file.dto.user.ModifyUserDTO;
import com.qiwenshare.file.util.SpringBeanUtil;
import com.qiwenshare.file.vo.department.DepartmentTreeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 部门表(Department)表控制层
 *
 * @author makejava
 * @since 2023-09-16 19:52:32
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {
    /**
     * 服务对象
     */
    @Resource
    private IDepartmentService departmentService;

    public static final String CURRENT_MODULE = "部门管理";

    @Resource
    private InitData initData;


    /**
     * 得到所有的部门
     *
     * @return
     */
    @Operation(summary = "得到所有的部门列表", description = "得到所有的部门列表", tags = {"部门管理"})
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public RestResult<List<DepartmentTreeVo>> selectDepartmentTree(@RequestParam(value = "departmentName", required = false) String departmentName) {
        return RestResult.success().data(departmentService.selectDepartmentTree(departmentName));
    }

    @Operation(summary = "新增组织机构", description = "新增组织机构", tags = {"部门管理"})
    @PostMapping(value = "/add")
    @MyLog(operation = "新增组织机构", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> add(@Valid @RequestBody AddDepartmentDTO addDepartmentDTO) {
        RestResult result = departmentService.insert(SpringBeanUtil.copyProperties(addDepartmentDTO, Department.class));
        initData.manualInitData();
        return result;
    }


    @Operation(summary = "编辑组织机构", description = "编辑组织机构", tags = {"部门管理"})
    @PostMapping(value = "/modify")
    @MyLog(operation = "编辑组织机构", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> modify(@Valid @RequestBody ModifyDepartmentDTO modifyDepartmentDTO) {
        RestResult result = departmentService.update(SpringBeanUtil.copyProperties(modifyDepartmentDTO, Department.class));
        initData.manualInitData();
        return result;
    }


    @Operation(summary = "删除组织机构", description = "删除组织机构", tags = {"部门管理"})
    @PostMapping(value = "/delete")
    @MyLog(operation = "删除组织机构", module = CURRENT_MODULE)
    @ResponseBody
    public RestResult<String> delete(@RequestBody DeleteDepartmentDTO deleteDepartmentDTO) {
        RestResult result = departmentService.deleteById(deleteDepartmentDTO.getDepartmentId());
        initData.manualInitData();
        return result;
    }

}

